/**
 * @description PNR录入JS
 * @author 赵子靖
 * @since 2018-07-06
 */
var PnrInput = function() {
    var t = {
        path :'',
        init : function() {
            t.initView();
            t.initEvent();
        },
        initView : function() {
            
        },
        initEvent : function() {
            
        },
        //初始化客户列表树
        initShopTypeTree:function(){
            ComTree.initTree($.extend({
                divContainer : "#customer-tree",
                url : t.path+'/customer/getCustomersTree.do',
                selectedFirstNode : true,
                p : {
                },
                onClick : function(e, treeId, treeNode) {
                    $("#shopType").val(treeNode.name);
                    $("#shopType").attr("ids",treeNode.id);
                }
            }));
        },
        //解析pnr
        parsePnr:function(){
            var loading = layer.load(3);
            var pnrContent = $("#pnrContent").val();
            if(pnrContent==null || pnrContent ==''){
                layer.close(loading);
                layer.alert("请输入需要解析的PNR", {
                    closeBtn: 0,
                    shift: 2 //动画类型
                });
                return;
            }
            var replaceNum = /\d+\./g;
            var reg = /\n(\n)*( )*(\n)*\n/g;
            pnrContent = pnrContent.replace(replaceNum,"\n");
            pnrContent = pnrContent.replace(reg,"\n");
            var pnrs = pnrContent.split('\n');
            if(pnrs!=null && pnrs.length>0){
                var spaceReg = /\s+/g;
                for(var row=0;row<pnrs.length;row++){
                    pnrs[row] = row+"."+pnrs[row];
                    pnrs[row] = pnrs[row].replace(spaceReg, ' ').split(" ");
                }
                pnrs.splice(0,1);
            }
            window.location.href=t.path+"/pnr/parserPnrContent.do?pnrContent="+"{'data':" + JSON.stringify(pnrs, "data") + "}";
        },
        //复制第一行某列内容到其他各行
        copyFirstContent:function(colIndex){
            var copyVal = $("#pnr-order-table tr:eq(1) td:eq("+colIndex+")").find("input").val();
            $("#pnr-order-table tr").each(function(i){
                if(i>1){
                    $(this).find("td:eq("+colIndex+")").find("input").val(copyVal);
                }
            });
        },
        //复制第一行某列内容到其他各行
        copyFirstSelectContent:function(colIndex){
            var copyVal = $("#pnr-order-table tr:eq(1) td:eq("+colIndex+")").find("select").val();
            $("#pnr-order-table tr").each(function(i){
                if(i>1){
                    $(this).find("td:eq("+colIndex+")").find("select").val(copyVal);
                }
            });
        },
        //客户选择对话框
        openCustomerDialog:function(){
            //TODO
        },
        //订单提交
        orderSubmit:function(){
            var loading = layer.load(3);
            //校验参数
            if(!t.verificationParamterData()){
                layer.close(loading);
                layer.alert("数据校验不通过，请检查。", {
                    closeBtn: 0,
                    shift: 2 //动画类型
                });
                return;
            }
            //获取提交参数
            var data = t.getOrderParamter();
            //alert("{'data':" + JSON.stringify(data, "data") + "}");
            $.ajax({
                type: "POST",
                url: t.path + "/order/bathCreateOrderForPnr.do",
                dataType: "json",
                data: {
                    "data": "{'data':" + JSON.stringify(data, "data") + "}"
                },
                success: function (data) {
                    layer.close(loading);
                    if(data.success){
                        //保存成功
                        layer.alert(data.msg, {
                            closeBtn: 0,
                            shift: 2 //动画类型
                        });
                        window.location.href=t.path+"/order/forwardOrderManager.do";
                        //跳转订单页面
                    }else{
                        //保存失败
                        layer.alert(data.msg, {
                            closeBtn: 0,
                            shift: 2 //动画类型
                        });
                    }
                },
                error:function(){
                    layer.close(loading);
                    //出错了
                    layer.alert("保存失败，出现异常。", {
                        closeBtn: 0,
                        shift: 2 //动画类型
                    });
                }
            });
        },
        //校验数据
        verificationParamterData:function(){
            var success = true;
            $("#pnr-order-table tr").each(function(i){
                if(i>0){
                    //客户
                    if(!t.verificationNotNull($(this).find("td:eq(5)").find("select").val(),$(this).find("td:eq(5)").find("select").attr("id"))){
                        success = false;
                    }
                    if(!t.verificationNumber($(this).find("td:eq(8)").find("input").val(),$(this).find("td:eq(8)").find("input").attr("id"))){
                        success = false;
                    }
                    if(!t.verificationNumber($(this).find("td:eq(9)").find("input").val(),$(this).find("td:eq(9)").find("input").attr("id"))){
                        success = false;
                    }
                    if(!t.verificationNumber($(this).find("td:eq(10)").find("input").val(),$(this).find("td:eq(10)").find("input").attr("id"))){
                        success = false;
                    }
                    if(!t.verificationNumber($(this).find("td:eq(11)").find("input").val(),$(this).find("td:eq(11)").find("input").attr("id"))){
                        success = false;
                    }
                    if(!t.verificationNumber($(this).find("td:eq(12)").find("input").val(),$(this).find("td:eq(12)").find("input").attr("id"))){
                        success = false;
                    }
                    if(!t.verificationNumber($(this).find("td:eq(13)").find("input").val(),$(this).find("td:eq(13)").find("input").attr("id"))){
                        success = false;
                    }
                    //出票日期
                    if(!t.verificationNotNull($(this).find("td:eq(14)").find("input").val(),$(this).find("td:eq(14)").find("input").attr("id"))){
                        success = false;
                    }
                }
            });
            return success;
        },
        verificationNotNull:function(val,inputId){
            if (val==null || val==''){
                layer.tips('不能为空', "#"+inputId,{tipsMore: true, tips: 3});
                return false;
            }
            return true;
        },
        verificationNumber:function(val,inputId){
            var regNumber = /^(-)?\d+(\.\d+)?$/;
            if (!regNumber.test(val)){
                layer.tips('请输入正确数值', "#"+inputId,{tipsMore: true, tips: 3});
                return false;
            }
            return true;
        },
        //获取提交参数
        getOrderParamter:function(){
            var data = [];
            $("#pnr-order-table tr").each(function(i){
                if(i>0){
                    //从第二行开始遍历取值
                    var order = {
                            "pnr":$(this).find("td:eq(1)").find("input").val(),
                            "name":$(this).find("td:eq(2)").find("input").val(),
                            "route":$(this).find("td:eq(3)").find("input").val(),
                            "ticketNo":$(this).find("td:eq(4)").find("input").val(),
                            "customerId":$(this).find("td:eq(5)").find("select option:selected").val(),
                            "ticketSource":$(this).find("td:eq(6)").find("select option:selected").val(),
                            "supplier":$(this).find("td:eq(7)").find("input").val(),
                            "ticketPrice":$(this).find("td:eq(8)").find("input").val(),
                            "ticketTax":$(this).find("td:eq(9)").find("input").val(),
                            "ticketSellPrice":$(this).find("td:eq(10)").find("input").val(),
                            "ticketSettlementPrice":$(this).find("td:eq(11)").find("input").val(),
                            "ticketRebate":$(this).find("td:eq(12)").find("input").val(),
                            "ticketProfit":$(this).find("td:eq(13)").find("input").val(),
                            "issueDate":$(this).find("td:eq(14)").find("input").val(),
                            "remark":$(this).find("td:eq(15)").find("input").val(),
                    }
                    data.push(order);
                }
            });
            return data;
        },
        //计算结算价
        calcSettlementPrice:function(row){
            //结算价=票价*（1-后返%）+税金
            var ticketPrice = $("#pnr-order-table tr:eq("+row+") td:eq(8)").find("input").val()*1;
            var rebate = $("#pnr-order-table tr:eq("+row+") td:eq(12)").find("input").val()*1;
            var tax = $("#pnr-order-table tr:eq("+row+") td:eq(9)").find("input").val()*1;
            var settlementPrice = Math.ceil((ticketPrice * (1-rebate*0.01) + tax ));
            $("#pnr-order-table tr:eq("+row+") td:eq(11)").find("input").val(settlementPrice);
            //结算价格变必须重新计算利润
            t.clacProfit(row);
        },
        //计算利润
        clacProfit:function(row){
            //利润=实收-结算
            var sellPrice = $("#pnr-order-table tr:eq("+row+") td:eq(10)").find("input").val()*1;
            var settlementPrice = $("#pnr-order-table tr:eq("+row+") td:eq(11)").find("input").val()*1;
            var profit = (sellPrice-settlementPrice).toFixed(2);
            $("#pnr-order-table tr:eq("+row+") td:eq(13)").find("input").val(profit);
        }
    }
    return t;
}();